//
//  PPSetMapAvatar.m
//  pet
//
//  Created by 陈无忌 on 12/8/14.
//  Copyright (c) 2014 whalefin. All rights reserved.
//

#import "PPSetMapAvatar.h"
#import "PPUserInfo.h"
#import "PPAPIList.h"
#import "PPSystemConfiguraton.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "UIImageView+UIActivityIndicatorForSDWebImage.h"

@implementation PPSetMapAvatar


+(UIImage *)getMapSelfPoint
{
    PPUserInfo *user = [PPSystemConfiguraton sharedSystemConfiguration].userInfo;
    NSString *url = [NSString stringWithFormat:@"%@/imgFile?fileName=%@!150",[PPAPIList getAPIList].rootUrl,user.userAvatar];
    NSURL *imageUrl = [NSURL URLWithString:url];
    UIImageView *imageView = [[UIImageView alloc] init];
//    UIImageView *avatar = [[UIImageView alloc] init];

    
    NSData *data = [NSData dataWithContentsOfURL:imageUrl];
    UIImage *avatar = [UIImage imageWithData:data];

    if (user.gender == 0) {
        [imageView setImage:[UIImage imageNamed:@"maleMap"]];
    }else{
        [imageView setImage:[UIImage imageNamed:@"femaleMap"]];
    }
    UIImage *clipAvatar = [PPSetMapAvatar circleImage:avatar];
    
    
    UIGraphicsBeginImageContext(imageView.image.size);
    [imageView.image drawInRect:CGRectMake(0, 0, 50, 76)];
    [clipAvatar drawInRect:CGRectMake(6, 7, 38, 38)];
    UIImage *resultImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return resultImg;
}

+ (UIImage *)circleImage:(UIImage*) image{
    
    UIGraphicsBeginImageContext(image.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 2);
    CGContextSetStrokeColorWithColor(context, [UIColor clearColor].CGColor);
    CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
    CGContextAddEllipseInRect(context, rect);
    CGContextClip(context);
    [image drawInRect:rect];
    CGContextAddEllipseInRect(context, rect);
    CGContextStrokePath(context);
    UIImage *circleImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return circleImg;
}

@end
